Xbasic

json_sanitize Function

Syntax

C result = json_sanitize(javascript as C [, method as L or C])

Arguments

javascriptCharacter

Javascript object notation input - doesn't require names be quoted, and allows for single quotes.

methodCharacter Logical

Default = .f.. Specifies how aggressive or what formatting method should be used when sanitizing the JSON. If set to .t., numeric data and other values that are not quoted will be quoted. If the JSON contains Javascript functions, specifying "A5" converts the functions to strings.

Description

Convert javascript object data to strict JSON, useful when copying snippets from javascript.

Converts a JSON string to 'strict' JSON syntax - Javascript objects can be created from literal definitions that do not follow the strict JSON syntax.

For example:

var obj = {name: 'Fred'}

However the JSON.parse() function can only parse strict JSON strings.

Similarly, on the server-side, the Xbasic json_parse() function can parse JSON strings that do not follow the strict syntax. However, many of the other Xbasic JSON functions (e.g. json_extract() ) require strict syntax.

The json_sanitize function will convert a JSON string to strict syntax

dim json as c = "{name: 'Fred'}"

?json_sanitize(json)
= {"name": "Fred"}